##################################################################
# /* Author: Gautam Nair and Nicholas Sambanis */
# /* Violence Exposure and Ethnic Identification: Evidence from Kashmir */
# /* Table 5:  Average Treatment Effects: Status, Integrative Institutions, Common Geography */

##################################################################

# change working directory here
# setwd("")

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(dplyr)
library(scales)

##################################################################

rm(list=ls())

library(stargazer)

data.working <- read.csv("nei_kashmir_replication_dataset.csv")

dep.var <- c("identity_rank_indian_rev", 
"identity_choice_indian_both", 
"kashmir_status_india",  
"ind_pak_prefer_ind",
"bonus_donation_india", 
"ic_index_id_no_ind_pak",
"ic_index_id_ind_pak",
"protest_peaceful_endorse",
"protest_violent_endorse"
)

ind.var <- c( "group3_growth" ,  "group4_int_inst", "group5_geography", "group_2345_violence")

data.working <- data.working[data.working$group2_violence!=1,]

reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))
for(i in 1:length(dep.var)){
	temp.data <- data.working[,c(dep.var[i],ind.var)]
	temp.data <- na.omit(temp.data)
	dep.var[i]
	my.formula <-paste(dep.var[i],'~', paste(ind.var, collapse= ' + '))
	temp.model <- lm(my.formula, data=temp.data)
	temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
	se.models[[i]] <- temp.se
	reg.models[[i]] <- temp.model
}

# stargazer customization

varlabels <-       c("Economic Growth", "Integrative Institutions", "Common Geography", "Violence Fixed Effect", "Constant (Control Group Mean)")
spectitle <- c("Average Treatment Effects: Economic Growth, Integrative Institutions, and Common Geography")
output.file <- c("tf_t_05_ate_status_geo_int_inst")
temptype <- c("latex", "text")
tempext <- c(".tex", ".txt")

for(q in 1:length(temptype)){
	temp.output <- paste(output.file, tempext[q], sep="")
	
 	stargazer(reg.models,se=se.models,
         title= spectitle,
          out=temp.output,
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=FALSE,
          digits=2,
         star.char=c("**", "***"),
         star.cutoffs=c(0.05, 0.01),
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = NULL,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
         type= temptype[q]
	) 
}








